<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<html>
<head>
	<title>不活跃客户开发</title>
	<meta name="decorator" content="default"/>
	<style type="text/css">
		#searchForm div.ui-input-text {
			padding: 0.4em;
			display:inline-block;
		}
		#timesDiv{
			display:inline-block;
		}
		#moneyDiv{
			display:inline-block;
		}
		.hide{
			display: none;
		}
		#searchForm .ui-radio{display:inline-block;vertical-align: middle;}
	</style>	
</head>
<body>
	<div data-role="navbar">
		<ul>
			<li><a class="leave-page" href="${ctx}/expert/customerExam/customer">专家方案筛选</a></li>
			<li><a href="#" class="ui-btn-active">不活跃客户开发</a></li>
		</ul>
	</div>
	<div class="icon-buttons">
		<a data-role="button" class="leave-page" data-inline="true" data-icon="back"  href="${ctx}" data-iconpos="notext">返回</a>
	</div>
	<tags:message content="${message}"/>
	<form id="searchForm"  action="${ctx}/expert/customerExam/filterCustomers" class="middleCls"  method="post">
			<label>开始日期：</label>
			<input type="date" id="startDate" data-inline="true" name="startDate" value="${startDate}"/>
			<label>结束日期：</label>
			<input type="date" id="endDate" data-inline="true" name="endDate" value="${endDate}"/><br/>
			<fieldset data-role="controlgroup" data-type="horizontal" data-inline="true">
		        <input type="radio"  id="times" name="type" value="times" ${empty type || type == 'times' ?  'checked' : ''}>
		        <label for="times">次数</label>
		        <input type="radio"  id="money" name="type" value="money" ${type == 'money' ?  'checked' : ''}>
		        <label for="money">金额</label>
			</fieldset> 
			<div id="timesDiv">
				<label for="maxTimes">来店次数上限： </label>
				<input type="number" id="maxTimes" min="0" name="maxTimes"  value="${not empty maxTimes ? maxTimes : 0}" style="width: 4em;"> &nbsp;&nbsp;次
			</div>
			<div id="moneyDiv">
				<label for="maxMoney">消费金额上限： </label>
				<input type="number" id="maxMoney" min="0" name="maxMoney" value="${not empty maxMoney ? maxMoney : 0}" style="width: 4em;">&nbsp;&nbsp;元
			</div>
			<input type="submit"  value="筛选" data-inline="true"/>
			<c:if test="${not empty customerList}">
				<input type="button"  data-inline="true" onclick="sendSmsToCustomers()" value="发送短信">
			</c:if>
	</form>
	<div class="form-input">
		<h3 class="middleCls">符合条件顾客列表</h3>
		<table id="contentTable" data-role="table" class="ui-responsive table-stroke">
			<thead><tr><th>客户姓名</th><th>备注名</th><th>手机号码</th>
			<c:if test="${type == 'money'}">
				<th>消费金额</th>
			</c:if>
			<c:if test="${type == 'times'}">
				<th>来店次数</th>
			</c:if>
			<th>操作</th></tr></thead>
			<tbody>
			<c:if test="${empty customerList}">
				<tr>
					<td colspan="5">没有符合条件的客户</td>
				</tr>
			</c:if>
			<c:if test="${not empty customerList}">
				<c:forEach items="${customerList}" var="customer">
					<tr id="${customer.id }" class="customerTr">
						<td>
							${customer.name}
						</td>
						<td>
							${customer.nickname}
						</td>
						<td>
							${customer.mobile}
						</td>
						<c:if test="${type == 'money'}">
							<td>${fns:getMoneyByCustomer(customer.id,startDate,endDate)}</td>
						</c:if>
						<c:if test="${type == 'times'}">
							<td>${fns:getTimesByCustomer(customer.id,startDate,endDate)}</td>
						</c:if>
						<td class="operateTD" id="${customer.id}">
							<input type="button" data-inline="true" data-iconpos="notext" data-icon="delete" value="删除"/>
						</td>
					</tr>
				</c:forEach>
			</c:if>
			</tbody>
		</table>
	</div>
	<script type="text/javascript">
		$(document).ready(function() {	
			$("#contentTable tbody tr").click(function(e){
				if(!this.id){
					return;
				}
				sessionStorage.target = "customerNotActive";
				window.location.href = "${ctx}/base/customer/detail?id=" + this.id;
			});
			$(".operateTD").click(function(){
				deleteCustomer(this.id);
			});
			//离开本模块时,清除使用过的session
			$(".leave-page").click(function(){
				var target = this.href;
				var url = "${ctx}/cleanSession?sessionKeys=startDate,endDate,type,maxMoney,maxTimes,session_not_active";
				var backFun = function(){
					window.location.href = target;
		 		};
		 		syncToServer(url, backFun);
				return false;
			});
			$(":radio").change(function(){
				if(this.value == "times"){
					$("#timesDiv").show();
					$("#moneyDiv").hide();
				}
				if(this.value == "money"){
					$("#timesDiv").hide();
					$("#moneyDiv").show();
				}
			});
			var check = $(":radio:checked").val();
			check == "times" ? $("#moneyDiv").hide(): $("#timesDiv").hide();
		 	jQuery("#searchForm").validate({
		 		submitHandler: function(form){
		 			var startValue = $("#startDate").val();
					if (!startValue){
						showTip("开始日期必须填写!");
						return;
					}
					var endValue = $("#endDate").val();
					if (!endValue){
						showTip("结束日期必须填写!");
						return;
					}
					if(startValue && endValue){
						 var start = new Date(startValue.replace(/-/g, "/"));
						 var end = new Date(endValue.replace(/-/g, "/"));
						 if (end < start) {
							showTip("结束日期不能大于开始日期!");
							return;
						}
					 }
					 if($(":checked").val() == 'money') {
						 if (isNaN($("#maxMoney").val())){
							 showTip("最大次数必须是数字!");
							 $("#maxMoney").val(0);
							 return;
						 }
					 } else {
						 if (isNaN($("#maxTimes").val())){
							 showTip("最大次数必须是数字!");
							 $("#maxTimes").val(0);
							 return;
						 }
					 }
					 loading("正在提交，请稍等...");
					 form.submit();
				}
	        });
		});
		function deleteCustomer(customerId){
			if (!customerId) {
				return;
			}
			var url = "${ctx}/expert/customerExam/deleteCustomer?target=notActive&customerId=" + customerId;
			var fun = function(){
				$("#" + customerId).attr("class",'hide');
				syncToServer(url);
			};
			global.confirm("确定移除此用户？",fun);
			this.event.stopPropagation();
		}
		function sendSmsToCustomers(){
			var tr = $(".customerTr");
			if (!tr || tr.length == 0) {
				showTip('没有符合用户！');
    		 	return;
				}
			var ids = ''; 
			if (tr && tr.length > 0) {
				for(var i = 0, len = tr.length; i < len; i ++){
					var c = tr[i];
	        		ids +=  c.id + ',';
				}
			}
       	  	if (ids){
				ids = ids.substring(0, ids.length - 1);
           	  	}
			var url = "${ctx}/sms/sendStoreGroupToCustomer/customerList?customerIds=" + ids + "&target=customerNotActive";
			window.location.href = url;
			}
	</script>
</body>
</html>
